package com.example.skillcinema.adapters

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.skillcinema.R
import com.example.skillcinema.databinding.CollectionItemBinding
import com.example.skillcinema.db.displaymodels.ProfileCollection
import com.example.skillcinema.db.localdb.tables.CollectionEntity
import com.example.skillcinema.viewholders.CollectionItemViewHolder

class CollectionRecyclerViewAdapter(
    val onClick: (id: Int, collectionName: String) -> Unit,
    val removeClick: (id: Int) -> Unit
) :
    RecyclerView.Adapter<CollectionItemViewHolder>() {
    private var values: List<ProfileCollection> = listOf()
    fun setData(list: List<ProfileCollection>) {
        values = list
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): CollectionItemViewHolder = CollectionItemViewHolder(
        CollectionItemBinding.inflate(
            LayoutInflater.from(parent.context), parent, false
        )
    )

    override fun onBindViewHolder(
        holder: CollectionItemViewHolder,
        position: Int
    ) {
        val item: ProfileCollection = values[position]
        with(holder.binding) {
            root.setOnClickListener {
                onClick(item.collectionId, item.name)
            }
            removeCollectionButton.setOnClickListener {
                removeClick(item.collectionId)
            }
            if (!item.isSystem) {
                removeCollectionButton.visibility = View.VISIBLE
            } else {
                removeCollectionButton.visibility = View.GONE
            }
            countTextView.text = item.count.toString()
            collectionIconImageView.setImageResource(
                when (item.type) {
                    CollectionEntity.CollectionType.FAVORITES -> R.drawable.ic_favorite
                    CollectionEntity.CollectionType.WATCHLIST -> R.drawable.ic_collection
                    CollectionEntity.CollectionType.WATCHED -> R.drawable.ic_seen
                    CollectionEntity.CollectionType.CUSTOM -> R.drawable.ic_profile
                }
            )
            nameTextView.text = item.name
        }
    }

    override fun getItemCount(): Int = values.count()
}